﻿using System;
using System.Linq;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.LucLuongThucTapForm;
using ComputerVision.PhuongTienThucTapForm;
using ComputerVision.Services;
using ComputerVision.Services.User;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.PhieuDieuTraCoSoForm
{
    public partial class frmPhuongAnCC : frmDetail
    {
        public Guid IdPhieu;
        private readonly IService<PhuongAnChuaChay> _phuongAnCCService;
        private readonly IService<LucLuongThucTap> _lucLuongService;
        private readonly IService<PhuongTienThucTap> _phuongTienService;
        private readonly IUserService<aspnet_Users> _userService;
        public frmPhuongAnCC()
        {
            InitializeComponent();
            _phuongAnCCService = Program.Container.Resolve<IService<PhuongAnChuaChay>>();
            _lucLuongService = Program.Container.Resolve<IService<LucLuongThucTap>>();
            _phuongTienService = Program.Container.Resolve<IService<PhuongTienThucTap>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            LoadLucLuong();
            LoadPhuongTien();
            Text = @"Thêm mới Phương án chữa cháy";
            dtpNgay.Focus();
        }

        public frmPhuongAnCC(Guid id)
        {
            InitializeComponent();
            _phuongAnCCService = Program.Container.Resolve<IService<PhuongAnChuaChay>>();
            _lucLuongService = Program.Container.Resolve<IService<LucLuongThucTap>>();
            _phuongTienService = Program.Container.Resolve<IService<PhuongTienThucTap>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            LoadLucLuong();
            LoadPhuongTien();
            Text = @"Sửa Phương án chữa cháy";
            dtpNgay.Focus();

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            var item = _phuongAnCCService.Detail(id);
            if (item != null)
            {
                dtpNgay.Value = item.Ngay;
                txtTinhHuong.Text = item.TinhHuong;
                cmbLucLuong.SelectedValue = item.LucLuongThucTapId;
                txtLucLuongSlg.Text = item.LucLuong_SoLuong.ToString();
                cmbPhuongTien.SelectedValue = item.PhuongTienThucTapId;
                txtPhuongTienSlg.Text = item.PhuongTien_SoLuong.ToString();
                txtKetQua.Text = item.KetQua;
                txtKienNghi.Text = item.KienNghi;
            }
        }

        #region Validation

        protected override bool Validator()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtTinhHuong.Text))
            {
                errorProvider.SetError(txtTinhHuong, "Bạn chưa nhập tình huống thực tập. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var model = new PhuongAnChuaChay
                {
                    Id = Guid.NewGuid(),
                    Ngay = dtpNgay.Value.Date,
                    TinhHuong = txtTinhHuong.Text,
                    LucLuongThucTapId = Guid.Parse(cmbLucLuong.SelectedValue.ToString()),
                    LucLuong_SoLuong = int.Parse(txtLucLuongSlg.Text),
                    PhuongTienThucTapId = Guid.Parse(cmbPhuongTien.SelectedValue.ToString()),
                    PhuongTien_SoLuong = int.Parse(txtPhuongTienSlg.Text),
                    KetQua = txtKetQua.Text,
                    KienNghi = txtKienNghi.Text,
                    PhieuDieuTraCoSoId = this.IdPhieu,
                    Created = DateTime.Now,
                    CreatedBy = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy = Constants.CurrentUser
                };
                _phuongAnCCService.Create(model);

            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var item = _phuongAnCCService.Detail(guid);
            if (item != null)
            {
                item.Ngay = dtpNgay.Value.Date;
                item.TinhHuong = txtTinhHuong.Text;
                item.LucLuongThucTapId = Guid.Parse(cmbLucLuong.SelectedValue.ToString());
                item.LucLuong_SoLuong = int.Parse(txtLucLuongSlg.Text);
                item.PhuongTienThucTapId = Guid.Parse(cmbPhuongTien.SelectedValue.ToString());
                item.PhuongTien_SoLuong = int.Parse(txtPhuongTienSlg.Text);
                item.KetQua = txtKetQua.Text;
                item.KienNghi = txtKienNghi.Text;
                //item.PhieuDieuTraCoSoId = this.IdPhieu;
                item.Modified = DateTime.Now;
                item.ModifiedBy = Constants.CurrentUser;
                _phuongAnCCService.Update(item);
            }
        }

        #endregion

        private void LoadLucLuong()
        {
            cmbLucLuong.DataSource = _lucLuongService.ToList().OrderBy(l => l.Ten).ToList();
            cmbLucLuong.DisplayMember = "Ten";
            cmbLucLuong.ValueMember = "Id";
        }

        private void LoadPhuongTien()
        {
            cmbPhuongTien.DataSource = _phuongTienService.ToList().OrderBy(p => p.Ten).ToList();
            cmbPhuongTien.DisplayMember = "Ten";
            cmbPhuongTien.ValueMember = "Id";
        }

        private void btnAddLucLuongThamGia_Click(object sender, EventArgs e)
        {
            using (var frm = new frmLucLuongThucTapDetail())
            {
                frm.OnLoadData += LoadLucLuong;
                frm.ShowDialog();
            }
        }

        private void btnAddPhuongTienThamGia_Click(object sender, EventArgs e)
        {
            using (var frm = new frmPhuongTienThucTapDetail())
            {
                frm.OnLoadData += LoadPhuongTien;
                frm.ShowDialog();
            }
        }
    }
}
